let { HttpRequest, globalData, UserAccess, mfutil, addUserStat } = getApp();
const app = getApp();

Page({
  data: {
    active: 0,   //0待完成,1未完成
    unfinishedList: [],
    finishedList: [],
    Unfpage: 1,
    Fpage: 1,
    oarr: [],
    jarr: [],
    router: '/pages/equity-page/equity-page',
    pagetitle: '权益中心列表页',
    loadingActive: false
  },
  onLoad(event) {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    // this.initPage()
    /**
* 用户行为记录埋点
*/
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let routerArr = getCurrentPages();
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
    }
    let track = UserAccess(UserAccessoptions)

    // 自定义埋点
    let customAccessoptions = {
      customType: '兑礼'
    }
    console.log('用户进入兑礼 Tab');

    let tracks = addUserStat(customAccessoptions)

  },
  onShow() {
    let { active } = this.data;
    if (active == 0) {
      this.unfinished('plush')


    } else if (active == 1) {
      this.finished('plush')

    }
  },
  saveSelect(ref) {
    console.log(ref, '11111111111111111');

    mfutil.tempConfig._refs.selectCard = ref;
  },
  onReachBottom() {
    let { active } = this.data;
    if (active == 0) {
      this.unfinished()
    } else if (active == 1) {
      this.finished()
    }
  },
  /**
   * 初始化页面
   */
  async initPage() {
    this.unfinished()
  },
  /**
   * 未完成
   * @param {*} index 
   */
  async unfinished(options) {
    let { Unfpage, unfinishedList, oarr, jarr } = this.data;
    if (options) {
      Unfpage = 1;
      unfinishedList = [];
      oarr = [];
      jarr = []
    }
    let bodyObj = {
    }
    let parmasObj = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
      category: 'redeem',
      page: Unfpage,
      size: 10
    }
    //未完成
    this.setData({
      loadingActive: true
    })
    let path1 = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/available_tiles`;
    let res1 = await HttpRequest(path1, 'POST', parmasObj, bodyObj);
    this.setData({
      loadingActive: false
    })
    let o = [],
      j = []
    if (res1.data) {
      res1.data.data.tiles.map((value, index) => {
        if (index % 2 == 0) {
          o.push(value)
        }
      })
      res1.data.data.tiles.map((value, index) => {
        if (index % 2 != 0) {
          j.push(value)
        }
      })
      this.setData({
        oarr: oarr,
        jarr: oarr,
      })
      this.setData({
        oarr: oarr.concat(o),
        jarr: jarr.concat(j),
        unfinishedList: res1.data ? unfinishedList.concat(res1.data.data.tiles) : null,
        Unfpage: res1.data.data.tiles.length > 0 ? Unfpage + 1 : Unfpage
      })
    }
  },
  /**
   * 已完成
   */
  async finished(options) {
    let { finishedList, Fpage } = this.data;
    if (options) {
      Fpage = 1;
      finishedList = [];
    }
    let bodyObj = {
    }
    let parmasObj = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
      category: 'redeem',
      page: Fpage,
      size: 10
    }
    this.setData({
      loadingActive: true
    })
    let path2 = '/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/completed_tiles';
    let res2 = await HttpRequest(path2, 'POST', parmasObj, bodyObj);
    console.log(res2, "快递单号");
    this.setData({
      loadingActive: false
    })
    if (res2.data) {
      this.setData({
        finishedList: finishedList
      }, () => {
        this.setData({
          finishedList: res2.data ? finishedList.concat(res2.data.data.tiles) : null,
          Fpage: res2.data.data.tiles.length > 0 ? Fpage + 1 : Fpage
        })
      })
    }
  },
  /**
   * 选项卡切换
   * @param {*} index 
   */
  selectcard(index) {
    let { active } = this.data;
    this.setData({
      active: index
    })

    if (this.data.active == 0) {
      // 自定义埋点
      let customAccessoptions = {
        customType: '兑礼-待兑换'
      }
      let tracks = addUserStat(customAccessoptions)
    } else {
      // 自定义埋点
      let customAccessoptions = {
        customType: '兑礼-已兑换'
      }
      let tracks = addUserStat(customAccessoptions)
    }
  },
  unfinishedTo(event) {
    let { activetype, item ,active} = event.currentTarget.dataset;
    let type = item.type;
    let url = '';
    if (type == 'survey' || type == 'progressive_profile' || type == 'contest' || type == 'exclusive_content' || type == 'progressive_profiling') {
      url = `/pages/getIntegral-detail/getIntegral-detail?activetype=${activetype}&id=${item.id}`
    } if (type == 'reward_sweeps' || type == 'digital_reward_redemption') {
      url = `/pages/equity-detail/equity-detail?id=${item.id}`
    } else if (type == 'physical_reward_redemption') {
      url = `/pages/conversion-page/conversion-page?id=${item.id}`
    } else if (type == 'post' || type == 'bonus_points' || type == 'birthday_bonus_points') {
      url = `/pages/member-privilege-detail/member-privilege-detail?id=${item.id}`
    } else if (type == 'exclusive_sweepstakes') {
      url = `/pages/equity-detail/equity-detail?id=${item.id}`
    } else if (type == 'lucky_draw') {
      url = `/pages/draw-prize/draw-prize?id=${item.id}`
    }
    
    my.navigateTo({
      url
    });

    // 自定义埋点
    let customAccessoptions = {
      customType: 'Tile点击兑礼',
      // tileId:item.tileId
    }
    console.log(item.title, '待兑换');

    let tracks = addUserStat(customAccessoptions)
  },
  finishedTo(event) {
    let { activetype, item,active } = event.currentTarget.dataset;
    let url = '';
    let type = item.type;
    if (type == 'survey' || type == 'progressive_profile' || type == 'contest' || type == 'exclusive_content' || type == 'progressive_profiling') {
      url = `/pages/getIntegral-detail/getIntegral-detail?activetype=${activetype}&id=${item.id}`
    } else if (type == 'reward_sweeps') {
      url = `/pages/equity-detail2/equity-detail2?id=${item.id}&islucky=${item.is_lucky}`
    } else if (type == 'digital_reward_redemption') {
      url = `/pages/equity-detail/equity-detail?url=${item.gift_url}&id=${item.id}&gifttype=${item.gift_type}&gifturl=${item.gift_url}`
    } else if (type == 'physical_reward_redemption') {
      let method = '';
      method = item.store_code ? 'pick' : 'expressage'
      url = `/pages/conversion-form/conversion-form?id=${item.id}&type=${type}&method=${method}&orderno=${item.order_no}&coupon_end_time=${item.coupon_end_time}&store_name=${item.store_name}&expressmobile=${item.express_mobile}&expressname=${item.express_name}&expressaddress=${item.express_address}&expresscompany=${item.express_company}&expressorderno=${item.express_order_no ? item.express_order_no : ''}&store_address=${item.store_address}&active=1`
    } else if (type == 'post' || type == 'bonus_points' || type == 'birthday_bonus_points') {
      url = `/pages/member-privilege-detail/member-privilege-detail?id=${item.id}&activetype=finished`
    } else if (type == 'exclusive_sweepstakes') {
      url = `/pages/equity-detail2/equity-detail2?id=${item.id}&islucky=${item.is_lucky}&method=member`
    } else if (type === 'lucky_draw') {  // 抽奖类型
      let type_prize = item.lucky_draw.prize_type;
      if (type_prize === 'digital_pic') {  // 单张图片
        url = `/pages/emoticon_page/emoticon_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&urls=${item.lucky_draw.prize_one_gift_url}`
      } else if (type_prize === 'store') { //门店自提
        let got = item.lucky_draw.got
        if (got == true) {
          let method = 'pick';
          url = `/pages/storeExpress-form/storeExpress-form?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&method=${method}&orderno=${item.order_no}&activetype=${active}&store_address=${item.store_address}&store_name=${item.store_name}&active=1&coupon_end_time=${item.coupon_end_time}&complete_text=${item.complete_text}`
        } else if (got == false) {
          url = `/pages/store_page/store_page?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
        }
      } else if (type_prize === 'express') { // 快递到家
        let got = item.lucky_draw.got
        if (got == true) {
          let method = 'expressage';
          url = `/pages/storeExpress-form/storeExpress-form?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&method=${method}&orderno=${item.order_no}&activetype=${active}&expressmobile=${item.express_mobile}&expressname=${item.express_name}&expressaddress=${item.express_address}&expresscompany=${item.express_company}&expressorderno=${item.express_order_no ? item.express_order_no : ''}&active=1`
        } else if (got == false) {
          url = `/pages/express_page/express_page?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
        }
      } else if (type_prize === 'digital_pdf') { // pdf图片
        url = `/pages/electron_prize/electron_prize?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&urls=${item.lucky_draw.prize_one_gift_url}`
      } else if (type_prize === 'digital_multi_pic') { // 多张图片
        url = `/pages/wallpaper_page/wallpaper_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&urls=${JSON.stringify(item.lucky_draw.prize_multi_gift_url)}`
      } else if (type_prize === 'coupon') { //  优惠卷
        url = `/pages/prize_page/prize_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'points') { // jifen
        url = `/pages/integrall_page/integrall_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'no_prize') { // 没中奖
        url = `/pages/no_prize/no_prize?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'ma') { // 指定ma
        url = `/pages/ma_page/ma_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'tile') { // 指定tile
        url = `/pages/tile_page/tile_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      }

    }
    
    my.navigateTo({
      url
    });

    // 自定义埋点
    let customAccessoptions = {
      customType: 'Tile点击兑礼',
      // tileId:item.tileId
    }
    console.log(item.title, '已兑换');

    let tracks = addUserStat(customAccessoptions)
  }
});
